﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Car : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strCarID = Request.QueryString["CarID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            FillDropDowns(db);
            if (strCarID != "add")
            {
                int CarID;
                if (!int.TryParse(strCarID, out CarID))
                    Response.Redirect("~/");
                DataAccessLayer.Car Car = DataAccessLayer.Car.FindByCarID_IncludeDeleted(db, CarID);
                if (Car == null)
                    Response.Redirect("~/");
                FromDatabase(Car);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Car.Convert(DataAccessLayer.Car.HistoryToListByCarID(db, CarID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strCarID = Request.QueryString["CarID"];
                string strRequestID = Request.QueryString["RequestID"];
                if (strCarID != "add")
                {
                    int CarID = int.Parse(strCarID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Car Car = DataAccessLayer.Car.FindByCarID_IncludeDeleted(db, CarID);
                    DataAccessLayer.Car NewCar = new DataAccessLayer.Car();
                    NewCar.CarID = CarID;
                    NewCar.EditorUserID = UserID;
                    NewCar.Timestamp = DateTime.Now;
                    ToDatabase(NewCar);
                    db.Cars.InsertOnSubmit(NewCar);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    int RequestID = int.Parse(strRequestID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sCar sCar = new DataAccessLayer.sCar();
                    sCar.RequestID = RequestID;
                    db.sCars.InsertOnSubmit(sCar);
                    db.SubmitChanges();
                    DataAccessLayer.Car NewCar = new DataAccessLayer.Car();
                    NewCar.CarID = sCar.ID;
                    NewCar.EditorUserID = UserID;
                    NewCar.Timestamp = DateTime.Now;
                    ToDatabase(NewCar);
                    db.Cars.InsertOnSubmit(NewCar);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strCarID = Request.QueryString["CarID"];
                int CarID = int.Parse(strCarID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Car Car = DataAccessLayer.Car.FindByCarID_IncludeDeleted(db, CarID);
                DataAccessLayer.Car NewCar = new DataAccessLayer.Car();
                NewCar.CarID = CarID;
                NewCar.EditorUserID = UserID;
                NewCar.Timestamp = DateTime.Now;
                NewCar.IsDeleted = true;
                ToDatabase(NewCar);
                db.Cars.InsertOnSubmit(NewCar);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Car Car)
        {
            txtLicensePlate.Text = Car.LicensePlate;
            txtDescription.Text = Car.Description;
            ddlCarColor.SelectedValue = Car.ColorID.ToString();
            ddlCarModel.SelectedValue = Car.CarModelID.ToString();
            lblDeletedItem.Visible = Car.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Car Car)
        {
            Car.LicensePlate = txtLicensePlate.Text;
            Car.Description = txtDescription.Text;
            Car.ColorID = int.Parse(ddlCarColor.SelectedValue);
            Car.CarModelID = int.Parse(ddlCarModel.SelectedValue);
        }

        public void FillDropDowns(DataAccessLayer.linqDatabaseDataContext db)
        {
            ddlCarModel.DataSource = DataAccessLayer.CarModel.ToList(db);
            ddlCarModel.DataTextField = "Name";
            ddlCarModel.DataValueField = "ID";
            ddlCarModel.DataBind();

            ddlCarColor.DataSource = DataAccessLayer.Color.ToList(db);
            ddlCarColor.DataTextField = "Name";
            ddlCarColor.DataValueField = "ID";
            ddlCarColor.DataBind();
        }
    }
}